/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package schedulerManagerment.Vacancy;

import Application.MarvellFrame;
import DataServices.LoadingBean;
import DataServices.TransactBean;
import DataServices.Validate;
import connection.Configuration;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Tngoc
 */
public class VacancyManager extends javax.swing.JFrame implements MarvellFrame {

    private ArrayList<Vacancy> vacancyList;
    private Connection conn;
    private DefaultTableModel model;
    private DefaultListModel listModelS;
    private DefaultListModel listModelV;

    private void userInit() {
        Configuration config = new Configuration();
        conn = config.getConnect();
        vacancyList = new ArrayList<>();
        model = (DefaultTableModel) jTable1.getModel();
        listModelS = new DefaultListModel();
        listModelV = new DefaultListModel();
        listSkill.setModel(listModelS);
        listVacancySkill.setModel(listModelV);
        LoadingBean loading = new LoadingBean();
        loading.loadComboBox("select name from field", comField);
        loading.loadComboBox("select name from Company", comCompany);
        loading.loadComboBox("select userName from Staff where role = 3", comInterviewer);
        getInfo();
    }

    private void getInfo() {
        jTable1.removeAll();
        for (int i = model.getRowCount() - 1; i >= 0; i--) {
            model.removeRow(i);
        }
        vacancyList.clear();
        ResultSet rs = null;
        try {
            Statement st = conn.createStatement();
            rs = st.executeQuery("showVacancy");
            while (rs.next()) {
                Vacancy va = new Vacancy();
                va.setId(rs.getInt("ID"));
                va.setCompany(rs.getString("company"));
                va.setField(rs.getString("field"));
                va.setAmount(rs.getInt("amount"));
                va.setDateOfPost(rs.getDate("dateOfPost"));
                va.setGender(rs.getString("gender"));
                va.setStatus(rs.getInt("status"));
                va.setInterviewer(rs.getString("interviewer"));
                vacancyList.add(va);
            }
            for (Vacancy vacancy : vacancyList) {
                Object[] obj = {vacancy.getId(), vacancy.getCompany(), vacancy.getField(), vacancy.getAmount(), vacancy.getDateOfPost(), vacancy.getGender(), vacancy.getStatus(), vacancy.getInterviewer()};
                model.addRow(obj);
            }

            rs.close();
            createSkill();
        } catch (SQLException ex) {
            Logger.getLogger(VacancyManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void createSkill() {
        listModelS.clear();
        listModelV.clear();
        try {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("select * from Skill");
            while (rs.next()) {
                listModelS.addElement(rs.getString("name"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(VacancyManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Creates new form VacancyManager
     */
    public VacancyManager() {
        initComponents();
        userInit();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtAmount = new javax.swing.JTextField();
        comGender = new javax.swing.JComboBox();
        comCompany = new javax.swing.JComboBox();
        comField = new javax.swing.JComboBox();
        btnSave = new javax.swing.JButton();
        btncancel = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        listSkill = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        listVacancySkill = new javax.swing.JList();
        btnListRemove = new javax.swing.JButton();
        btnListAdd = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        comInterviewer = new javax.swing.JComboBox();
        btnAddSave = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "company", "field", "amount", "date", "gender", "status", "interviewer"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("infomation"));

        jLabel1.setText("company");

        jLabel2.setText("field");

        jLabel3.setText("amount");

        jLabel5.setText("gender");

        txtAmount.setEditable(false);
        txtAmount.setEnabled(false);

        comGender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "both", "male", "female" }));
        comGender.setEnabled(false);

        comCompany.setEnabled(false);
        comCompany.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                comCompanyPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        comField.setEnabled(false);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/arrow-left-blue.png"))); // NOI18N
        btnSave.setText("save");
        btnSave.setEnabled(false);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btncancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Minus Red Button.png"))); // NOI18N
        btncancel.setText("cancel");
        btncancel.setEnabled(false);
        btncancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelActionPerformed(evt);
            }
        });

        listSkill.setEnabled(false);
        listSkill.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listSkillMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(listSkill);

        listVacancySkill.setEnabled(false);
        listVacancySkill.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listVacancySkillMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(listVacancySkill);

        btnListRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/arrow-right-blue.png"))); // NOI18N
        btnListRemove.setEnabled(false);
        btnListRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListRemoveActionPerformed(evt);
            }
        });

        btnListAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/arrow-left-blue.png"))); // NOI18N
        btnListAdd.setEnabled(false);
        btnListAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListAddActionPerformed(evt);
            }
        });

        jLabel4.setText("interviewer");

        comInterviewer.setEnabled(false);

        btnAddSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/arrow-down-blue.png"))); // NOI18N
        btnAddSave.setText("Add");
        btnAddSave.setEnabled(false);
        btnAddSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAddSave, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btncancel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(comInterviewer, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(comGender, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(comCompany, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(comField, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnListAdd)
                            .addComponent(btnListRemove))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(35, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(comCompany, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(comField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(comInterviewer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnListAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnListRemove))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btncancel)
                    .addComponent(btnAddSave, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/arrow-right-blue.png"))); // NOI18N
        btnEdit.setText("edit");
        btnEdit.setEnabled(false);
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Button White Load.png"))); // NOI18N
        btnRefresh.setText("refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Button White Add.png"))); // NOI18N
        btnAdd.setText("add new");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Import Document.png"))); // NOI18N
        btnBack.setText("back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnBack)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRefresh)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEdit))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 591, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnRefresh)
                            .addComponent(btnAdd)
                            .addComponent(btnBack)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnListRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListRemoveActionPerformed
        if (listVacancySkill.getSelectedIndex() != -1) {
            Vacancy vac = getVacancyById(Integer.parseInt(model.getValueAt(jTable1.getSelectedRow(), 0).toString()));
            Skill s = getVacancySkill(vac, listVacancySkill.getSelectedValue().toString());
            vac.getSkills().remove(s);
            listModelS.addElement(listVacancySkill.getSelectedValue());
            listModelV.removeElement(listVacancySkill.getSelectedValue());
            btnListRemove.setEnabled(false);
        }
    }//GEN-LAST:event_btnListRemoveActionPerformed

    private Skill getVacancySkill(Vacancy vac, String name) {
        for (Skill s : vac.getSkills()) {
            if (s.getName().equals(name)) {
                return s;
            }
        }
        return null;
    }

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:

        if (jTable1.getSelectedRow() != -1) {
            comField.addItem(model.getValueAt(jTable1.getSelectedRow(), 2));
            comCompany.setSelectedItem(model.getValueAt(jTable1.getSelectedRow(), 1));
            comField.setSelectedItem(model.getValueAt(jTable1.getSelectedRow(), 2));
            txtAmount.setText(model.getValueAt(jTable1.getSelectedRow(), 3).toString());
            comGender.setSelectedItem(model.getValueAt(jTable1.getSelectedRow(), 5));
            Vacancy vac = getVacancyById(Integer.parseInt(model.getValueAt(jTable1.getSelectedRow(), 0).toString()));
            createSkill();
            for (Skill skill : vac.getSkills()) {
                listModelV.addElement(skill.getName());
                listModelS.removeElement(skill.getName());
            }
            comInterviewer.setSelectedItem(model.getValueAt(jTable1.getSelectedRow(), 7));
        }
        btnEdit.setEnabled(true);
    }//GEN-LAST:event_jTable1MouseClicked

    private void btnListAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListAddActionPerformed
        if (listSkill.getSelectedIndex() != -1) {
            listModelV.addElement(listSkill.getSelectedValue());
            Vacancy vac = getVacancyById(Integer.parseInt(model.getValueAt(jTable1.getSelectedRow(), 0).toString()));
            vac.getSkills().add(getSkillByName(listSkill.getSelectedValue().toString()));
            listModelS.removeElement(listSkill.getSelectedValue());
            btnListAdd.setEnabled(false);
        }
    }//GEN-LAST:event_btnListAddActionPerformed

    private Skill getSkillByName(String name) {
        try {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("select ID from Skill where name = '" + name + "'");
            rs.next();
            Skill sk = new Skill(rs.getInt("ID"), name);
            return sk;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }
    private void listSkillMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listSkillMouseClicked
        if (listSkill.getSelectedIndex() != -1) {
            btnListRemove.setEnabled(false);
            btnListAdd.setEnabled(true);
        }
    }//GEN-LAST:event_listSkillMouseClicked

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        setEnnable(true);
        btnSave.setEnabled(true);
        btnEdit.setEnabled(false);
        btnAdd.setEnabled(false);
    }//GEN-LAST:event_btnEditActionPerformed

    private void listVacancySkillMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listVacancySkillMouseClicked
        if (listVacancySkill.getSelectedIndex() != -1) {
            btnListAdd.setEnabled(false);
            btnListRemove.setEnabled(true);
        }
    }//GEN-LAST:event_listVacancySkillMouseClicked

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        getInfo();
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btncancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelActionPerformed
        setEnnable(false);
        btnAddSave.setEnabled(false);
        btnSave.setEnabled(false);
        btnEdit.setEnabled(true);
        btnAdd.setEnabled(true);
        getInfo();
    }//GEN-LAST:event_btncancelActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        setEnnable(true);
        btnAddSave.setEnabled(true);
        btnAdd.setEnabled(false);
        btnEdit.setEnabled(false);
        listSkill.setEnabled(false);
        listVacancySkill.setEnabled(false);
        jTable1.clearSelection();
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnAddSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddSaveActionPerformed
        try {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("select ID from Company where name = '" + comCompany.getSelectedItem().toString() + "'");
            rs.next();
            int comID = rs.getInt("ID");
            rs.close();
            rs = st.executeQuery("select ID from Field where name = '" + comField.getSelectedItem().toString() + "'");
            rs.next();
            int fieldID = rs.getInt("ID");
            rs.close();
            rs = st.executeQuery("select ID from Staff where userName = '" + comInterviewer.getSelectedItem().toString() + "'");
            rs.next();
            int interID = rs.getInt("ID");
            rs.close();
            String gd = "NULL";
            switch (comGender.getSelectedItem().toString()) {
                case "male":
                    gd = "1";
                    break;
                case "female":
                    gd = "0";
                    break;
            }
            SimpleDateFormat dateformat = new SimpleDateFormat("MM-dd-yyyy");
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 2);
            Date dateNow = cal.getTime();
            String date = new StringBuilder(dateformat.format(dateNow)).toString();
            if(new Validate().isNumber(txtAmount.getText())){
                String qry = "insert into Vacancy values(" + comID + "," + fieldID + "," + txtAmount.getText() + ",'" + date + "'," + gd + ",0," + interID + ")";
                st.executeUpdate(qry);
            }
            getInfo();
            btncancel.doClick();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnAddSaveActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try {
            Vacancy vac = getVacancyById(Integer.parseInt(model.getValueAt(jTable1.getSelectedRow(), 0).toString()));
            Statement st = conn.createStatement();
            String gd = "NULL";
            switch (comGender.getSelectedItem().toString()) {
                case "male":
                    gd = "1";
                    break;
                case "female":
                    gd = "0";
                    break;
            }
            st.executeUpdate("updateVac " + vac.getId() + ",'" + comCompany.getSelectedItem().toString() + "','" + comField.getSelectedItem().toString() + "','" + comInterviewer.getSelectedItem().toString() + "'");
            st.executeUpdate("update Vacancy set gender=" + gd + ", amount =" + txtAmount.getText() + " where ID = " + vac.getId());
            st.executeUpdate("delete from VacancySkill_Detail where VacancyID = " + vac.getId());
            for (Skill skill : vac.getSkills()) {
                String qry = "insert into VacancySkill_Detail values(" + vac.getId() + "," + skill.getId() + ")";
                st.executeUpdate(qry);
            }
            btncancel.doClick();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void comCompanyPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_comCompanyPopupMenuWillBecomeInvisible
        // TODO add your handling code here:
        LoadingBean loading = new LoadingBean();
        comField.removeAllItems();
        loading.loadComboBox("select name from field", comField);
        try {
            Statement st = conn.createStatement();
            String companString = comCompany.getSelectedItem().toString();
            ResultSet rs = st.executeQuery("select Field.name as name from Field,Vacancy,Company where Field.ID =Vacancy.field and Vacancy.companyID = Company.ID and Company.name = '" + companString + "'");
            while (rs.next()) {
                String field = rs.getString("name");
                comField.removeItem(field);
            }
            int select =jTable1.getSelectedRow();
            if(select!=-1){
            comField.addItem(model.getValueAt(select, 2));
            }
        } catch (SQLException ex) {
            Logger.getLogger(VacancyManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_comCompanyPopupMenuWillBecomeInvisible

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        this.exitFrame();        // TODO add your handling code here:
    }//GEN-LAST:event_btnBackActionPerformed

    private Vacancy getVacancyById(int id) {
        for (Vacancy vacancy : vacancyList) {
            if (id == vacancy.getId()) {
                return vacancy;
            }
        }
        return null;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnAddSave;
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnListAdd;
    private javax.swing.JButton btnListRemove;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btncancel;
    private javax.swing.JComboBox comCompany;
    private javax.swing.JComboBox comField;
    private javax.swing.JComboBox comGender;
    private javax.swing.JComboBox comInterviewer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JList listSkill;
    private javax.swing.JList listVacancySkill;
    private javax.swing.JTextField txtAmount;
    // End of variables declaration//GEN-END:variables

    @Override
    public void startFrame(JFrame app) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VacancyManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        this.root = app;
        app.setEnabled(false);
        this.setVisible(true);
    }

    @Override
    public void exitFrame() {
        root.setEnabled(true);
        this.setVisible(false);
    }
    private JFrame root;

    private void setEnnable(boolean flag) {
        comCompany.setEnabled(flag);
        comField.setEnabled(flag);
        comGender.setEnabled(flag);
        txtAmount.setEditable(flag);
        txtAmount.setEnabled(flag);
        listSkill.setEnabled(flag);
        listVacancySkill.setEnabled(flag);
        btncancel.setEnabled(flag);
        comInterviewer.setEnabled(flag);
        jTable1.setEnabled(!flag);
        btnAdd.setEnabled(!flag);
        btnEdit.setEnabled(!flag);
        btnRefresh.setEnabled(!flag);
    }
}
